package com.spd.shop.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.spd.shop.service.IShopService;
import com.spd.shop.entity.Shop;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孙沛栋
 * @since 2023-03-19
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Resource
    private IShopService shopService;

    //新增或者更新
    @PostMapping
    public boolean save(@RequestBody Shop shop) {
        return shopService.saveOrUpdate(shop);
    }

    //删除
    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return shopService.removeById(id);
    }

    //批量删除
    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return shopService.removeBatchByIds(ids);
    }

    //查询所有数据
    @GetMapping
    public List<Shop> findAll() {
        return shopService.list();
    }
    //根据id查询
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable Integer id) {
        return shopService.getById(id);
    }
    //分页查询
    @GetMapping("/page")
    public Page<Shop> findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String shopNum,
                               @RequestParam(defaultValue = "") String area,
                               @RequestParam(defaultValue = "") String rent,
                               @RequestParam(defaultValue = "") String landlordNum,
                               @RequestParam(defaultValue = "") String landlordName,
                               @RequestParam(defaultValue = "") String landlordPhone,
                               @RequestParam(defaultValue = "") String state,
                               @RequestParam(defaultValue = "") String createTime
    ) {
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(shopNum)) {
            queryWrapper.like("shop_num", shopNum);
        }
        if (!"".equals(area)) {
            queryWrapper.like("area", area);
        }
        if (!"".equals(rent)) {
            queryWrapper.like("rent", rent);
        }
        if (!"".equals(landlordNum)) {
            queryWrapper.like("landlord_num", landlordNum);
        } if (!"".equals(landlordName)) {
            queryWrapper.like("landlord_name", landlordName);
        } if (!"".equals(landlordPhone)) {
            queryWrapper.like("landlord_phone", landlordPhone);
        }
        if (!"".equals(state)) {
            queryWrapper.like("state", state);
        }
        if (!"".equals(createTime)) {
            queryWrapper.like("create_time", createTime);
        }

        return shopService.page(new Page<>(pageNum, pageSize),queryWrapper);
    }




}

